clearvars -except Globaloption option

if exist('Globaloption', 'var') == 0
    Globaloption.savefig = 1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% read data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

data = readtable('../../data/supplement/UKgiltreturn.xlsx', 'Sheet', 'UKgovernmentbondreturn');
date = data.year;

startdatenum = 1800;
enddatenum = 1914;

startdate = find(date == startdatenum);
enddate = find(date == enddatenum);
T_post = length(date(startdate:enddate));

Tstart = startdate; 
Tend = enddate; 
T = Tend - Tstart + 1;

date = date(startdate:enddate);

data = data(((data.year) >= startdatenum), :);
data = data(((data.year) <= enddatenum), :);
realreturn = data.Return ./ (1 + data.Inflation);

cumreturn = exp(cumsum(log(realreturn)));
cumreturn1 = cumreturn / cumreturn(1);
date1 = date;

%% 2nd data

data = readtable('../../data/supplement/UKgiltreturn.xlsx', 'Sheet', 'UKgovernmentbondreturn');
date = data.year;

startdatenum = 1914;
enddatenum = 2000;

startdate = find(date == startdatenum);
enddate = find(date == enddatenum);
T_post = length(date(startdate:enddate));

Tstart = startdate; 
Tend = enddate; 
T = Tend - Tstart + 1;

date = date(startdate:enddate);

data = data(((data.year) >= startdatenum), :);
data = data(((data.year) <= enddatenum), :);
realreturn = data.Return ./ (1 + data.Inflation);

cumreturn = exp(cumsum(log(realreturn)));
cumreturn2 = cumreturn / cumreturn(1);
date2 = date;

f = figure;
f.Position = [100 100 800 300];
subplot(1, 2, 1);
plot(date1, cumreturn1, 'k', LineWidth = 2)
hold on
ylabel('Cumulative Real Return')
xlabel('Year ')
grid on
xlim([min(date1) max(date1)])

subplot(1, 2, 2);
plot(date2, cumreturn2, 'k', LineWidth = 2)
hold on
ylabel('Cumulative Real Return')
xlabel('Year ')
grid on
xlim([min(date2) max(date2)])

filename = strcat('../../results/figures_main/UK/giltreturn');

if Globaloption.savefig == 1
    print(filename, '-dpng', '-r1000');
end
